package com.jwm.power.service.impl;

import com.jwm.power.mapper.InquiryMapper;
import com.jwm.power.model.common.EnumResult;
import com.jwm.power.model.entity.InquiryT;
import com.jwm.power.model.entity.T6Freight;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.repository.InquiryRepository;
import com.jwm.power.repository.T6FreightRepository;
import com.jwm.power.service.T6FreightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;


@Service
@Transactional
public class T6FreightServiceImpl implements T6FreightService {
    @Autowired
    @Qualifier("transactionManager2")
    private PlatformTransactionManager transactionManager2;

    @Autowired
    private T6FreightRepository t6FreightRepository;
    @Autowired
    private InquiryRepository inquiryRepository;
    @Autowired
    private InquiryMapper inquiryMapper;
    @Override
    public ResponseData getT6Freight() {
        AtomicReference<T6Freight> t6SourceResult = new AtomicReference<>();
        try {
            TransactionTemplate template = new TransactionTemplate(transactionManager2);
            template.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                    // 查询所有 InquiryT 记录
                    List<InquiryT> inquiries = inquiryRepository.findAll();

                    for (InquiryT model_T : inquiries) {
                        String company = model_T.getCompany();
                        Date startDate = model_T.getInquirydate();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(startDate);
                        calendar.add(Calendar.MONTH, -1);
                        Date endDate = calendar.getTime();

                        // 查询 T6Freight 数据
                        List<T6Freight> t6FreightList = t6FreightRepository.findT6FreightByInquirydateBetweenAndCompany(startDate, endDate, company);

                        // 更新数据库
                        for (T6Freight t6Freight : t6FreightList){
                            Map<String, Object> tmap = new HashMap<>();
                            tmap.put("tName", "inquirydata");
                            tmap.put("values", t6Freight);
                            tmap.put("strWhere", " where company='" + company + "' and inquirydate between '" + startDate + "' and '" + endDate + "'");
                            inquiryMapper.updateDefined(tmap);
                        }
                    }
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
        // 通过这个方式，可以实现从第二个数据源获取到数据，然后添加到这个原子操作类上，最后返回的时候从操作类里获取到对象，返回的就是【T6Freight】
        return new ResponseData(EnumResult.SUCCESS.getCode(), "success", t6SourceResult.get());
    }

}